
function xi = xi_BLP(B,xi0,S,X,A,nu,wnu) % BLP contraction mapping algorithm to match predicted and observed vote shares

K = size(X,2);

xi = exp(xi0);

diff = 1;

while diff > 0
    s = exp(X * B(1:K,1) + B(K+1,1) * X(:,K-1) * nu(:,1)' + B(K+2,1) * X(:,K) * nu(:,2)') .* xi;
    s = (s ./ (1 + A * s)) * wnu;
    xi1 = xi .* (S ./ s);
    diff = max(abs(s - S));
    diff = diff > 10^(-14);
    xi = xi1; 
end

xi = log(xi);


    
    